package com.lxq.www.aspect;

import com.lxq.www.aspect.dao.RequestLogDao;
import com.lxq.www.aspect.logentities.RequestLog;
import org.aspectj.lang.JoinPoint;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

//@Aspect
//@Component
public class RequestAspect {

    //@Autowired
    private RequestLogDao logDao;

    //@Pointcut(value = "execution(* com.lxq.cloud.controller.*.*(..))")
    public void requestRecord(){}

    //@After(value = "requestRecord()")
    public void afterMethod(JoinPoint joinPoint){
        ServletRequestAttributes requestAttributes= (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest httpServletRequest=requestAttributes.getRequest();
        RequestLog log=new RequestLog();
        log.setId(0);
        log.setRequestMethod(joinPoint.getSignature().getName());
        log.setRequestDate(new SimpleDateFormat("YYYY-MM-dd HH:mm:ss").format(new Date()));
        log.setRequestIp(httpServletRequest.getRemoteAddr());
        log.setMethodArgs(Arrays.toString(joinPoint.getArgs()));
        log.setRequestURL(httpServletRequest.getRequestURI());
        System.out.println(log.toString());
        logDao.save(log);
    }
}
